﻿namespace ChinaUnicom.Parser.Parsers
{
    using ChinaUnicom.Models;
    using HtmlAgilityPack;
    using System.Collections.Generic;
    using System.Linq;

    internal class PackageParser : IItemsParser
    {
        public ItemType Type
        {
            get { return ItemType.Package; }
        }

        public ItemCollection Parse(string html)
        {
            ItemCollection items = new ItemCollection();

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html.Replace("&nbsp;", string.Empty));

            var node = doc.DocumentNode;// .ChildNodes.Select(x=>x.Attributes["class"].Value == "nrong")

            var divs = node.Descendants("div").Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "nrong").ToList();

            var basic = divs[0].Descendants("p").FirstOrDefault();
            if (basic != null)
            {
                string name = string.Empty;
                foreach (HtmlNode span in basic.ChildNodes.Where(x => x.Name == "span"))
                {
                    if (string.IsNullOrEmpty(name))
                    {
                        name = span.InnerText.InnerTextTrim();
                    }
                    else
                    {
                        items.Add(name, span.InnerText.InnerValueTrim());
                        name = string.Empty;
                    }
                }
            }

            Item more = null;
            foreach (HtmlNode inode in divs[1].ChildNodes)
            {
                if (inode.Name == "#text" && more == null)
                {
                    var n = inode.InnerText.InnerTextTrim();
                    if (!string.IsNullOrEmpty(n))
                    {
                        more = new Item();
                        more.Name = inode.InnerText.InnerTextTrim();
                        items.Add(more);
                    }
                }

                if (inode.Name == "span")
                {
                    more.Value = inode.InnerText.InnerValueTrim();
                }

                if (inode.Name == "div" && inode.Attributes["class"] != null && inode.Attributes["class"].Value == "mrlf")
                {
                    more.Details.Add(GetPackagesChildren(inode));
                    more = null;
                }
            }

            return items;
        }

        static IEnumerable<Item> GetPackagesChildren(HtmlNode node)
        {
            foreach (HtmlNode item in node.ChildNodes)
            {
                if (item.Name == "#text")
                {
                    var texts = item.InnerText.InnerValueTrim().Split(new[] { '：' });
                    if (texts.Length >= 2)
                    {
                        var i = new Item();
                        i.Name = texts[0].InnerValueTrim();
                        i.Value = texts[1].InnerValueTrim();

                        yield return i;
                    }
                }
            }
        }
    }
}
